layui.use(['table', 'form', 'layer', 'laydate', 'laytpl', 'element'], function() {
	var table = layui.table,
		optionTab = layui.table,
		form = layui.form,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		element = layui.element;
	
	/*加载查询处维度下拉框*/
	$.ajax({
		type : 'get',
		url : '../dimensionmanagement/getalldimensionlist',
		datatype : 'json',
		success : function(dimension) {
			if (dimension.code == 0) {
				var str = "";
				for(var i = 0; i < dimension.count; i++){
					str += '<option value=' + dimension.data[i].dimensionid + '>'+ dimension.data[i].dimensionName+'</option>';
				}
				$("#selDimension").append(str);
				form.render("select");
			}
		}
	});
	
	form.on('select(selDimension)', function(data) {
		var dimension = data.value;
		if(dimension == "00") {
			dimension = "";
		}
		if(dimension == ""){
			layer.msg("请选择维度！");
			return;
		}
		/*动态加载修改题目信息处卷类下拉框*/
		$("#selvolClass").html("");
		$.ajax({
			type: 'get',
			url: '../scaletitletype/getScaletitletypebydropdown?dimensionid=' + dimension,
			datatype: 'json',
			success: function(voldata) {
				if(voldata.code == 0) {
					var str = "<option value='00'>请选择卷类</option>";
					for(var i = 0; i < voldata.count; i++) {
						str += '<option value=' + voldata.data[i].id + '>' + voldata.data[i].name + '</option>';
					}
					$("#selvolClass").append(str);
					form.render("select");
				} else {
					$("#selvolClass").html("");
					layer.msg('暂无对应的卷类', {
						icon: 2
					});
					form.render("select");
				}
			}
		});
	});
	
	table.render({
		elem: '#titleList',
		id:'titleid',
		url : '../titlemanagement/getalltitlelist',
		title: '题目表',
		skin: 'line',
		height: 'full-130',
		even: true,
		cols: [
			[{
				field: 'titleName',
				title: '题目名称',
				align: 'center',
				width: '10%'
			},{
				field: 'dimensionName',
				title: '所属维度',
				align: 'center',
				width: '14%'
			}, {
				field: 'name',
				title: '所属卷类',
				align: 'center',
				width: '7%'
			}, {
				field: 'optionNumber',
				title: '选项数量',
				align: 'center',
				width: '7%'
			}, {
				field: 'optionContent',
				title: '选项内容',
				align: "center",
				width: '20%'
			}, {
				field: 'titleDescribe',
				title: '题目描述',
				align: 'center',
				width: '20%'
			},{
				field: 'isReverse',
				title: '是否反选',
				toolbar: '#barDemoElection',
				align: 'center',
				width: '8%'
			}, {
				title: '操作',
				toolbar: '#barDemo',
				align: "center",
				width: '14%'
			}]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit: 10,
			limits: [10, 20, 30, 40, 50],
			theme: '#1E9FFF',						
		}
	});
	
	$("#selclass").click(function(){
		var selDimension = $("#selDimension").val();
		var seltilType = $("#seltilType").val();
		var selvolClass = $("#selvolClass").val();
		if(selDimension == "00" || selDimension == null){
			selDimension = "";
		}
		if(seltilType == "00" || seltilType == null){
			seltilType = "";
		}
		if(selvolClass == "00" || selvolClass == null){
			selvolClass = "";
		}
		var seltitleName = $.trim($("#seltitleName").val());
		table.render({
			elem: '#titleList',
			id:'titleid',
			url : '../titlemanagement/getalltitlelist?dimensionid='+selDimension+"&titleType="+seltilType + "&titleName="+seltitleName + "&titletypeid="+selvolClass,
			title: '题目表',
			skin: 'line',
			height: 'full-130',
			even: true,
			cols: [
				[{
					field: 'titleName',
					title: '题目名称',
					align: 'center',
					width: '10%'
				},{
					field: 'dimensionName',
					title: '所属维度',
					align: 'center',
					width: '14%'
				}, {
					field: 'name',
					title: '所属卷类',
					align: 'center',
					width: '7%'
				}, {
					field: 'optionNumber',
					title: '选项数量',
					align: 'center',
					width: '7%'
				}, {
					field: 'optionContent',
					title: '选项内容',
					align: "center",
					width: '20%'
				}, {
					field: 'titleDescribe',
					title: '题目描述',
					align: 'center',
					width: '20%'
				},{
					field: 'isReverse',
					title: '是否反选',
					toolbar: '#barDemoElection',
					align: 'center',
					width: '8%'
				}, {
					title: '操作',
					toolbar: '#barDemo',
					align: "center",
					width: '14%'
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			}
		});
	});
	
	//监听行工具事件
	table.on('tool(titleList)', function(obj) {
		var data = obj.data;
		/*为题目添加选项*/
		if(obj.event === 'addopt') {
			/*djf添加*/
			$("#inpoptiontitleid").val(data.titleid);
			$("#inpdimensionName").text(data.titleName);
			var index = layer.open({
		        type: 1, 
		        title: '选项信息添加',
		        area: ['850px', '550px'],
		        shade: 0.8,
		        content: $('#addoptionsion'),
		        maxmin: true,
		    });
	    	layer.full(index);
	    	
	    	/*选项信息列表-表格*/
	    	table.render({
				elem: '#taboptioncon',
				id:'taboptioncon',
				url: '../titlemanagement/getoptionbytitle?titleid='+data.titleid,
				title: '选项信息表表',
				skin: 'line',
				even: true,
				cols: [
					[{
						field: 'optionNumber',
						title: '选项序号'
					}, {
						field: 'optionContent',
						title: '选项内容'
					}, {
						field: 'optionScore',
						title: '选项分数'
					}, {
						title: '操作',
						toolbar: '#barDemooption'
					}]
				]
			});
		}
		/*查看班级信息详情*/
		if(obj.event === 'btnselclass') {
			$("#txtTitleId").text(data.titleid);
			$("#txtTitleName").text(data.titleName);
			$("#txtTitleType").text(data.titleType);
			$("#txtDimension").text(data.dimensionid);
			$("#txttilNumber").text(data.titleNumber);
			$("#txtoptNumber").text(data.optionNumber);
			$("#txtoptContent").text(data.optionContent);
			$("#txtDescribe").text(data.titleDescribe);
			
			layer.open({
		        type: 1, 
		        title: '题目信息详情',
		        area: ['666px', '420px'],
		        shade: 0.8,
		        content: $('#titledet'),
		        btn: ['返回'], 
		        yes: function(){
		          layer.closeAll();
		          $(".selTitle-con").css("display","none");
		        },
		        cancel: function(){ 
				   $(".selTitle-con").css("display","none");
				}
		    });
		}else if(obj.event === 'edit') {   //修改题目信息
			$("#editTitleId").val(data.titleid);
			$("#editTitleName").val(data.titleName);
			$("#editSort").val(data.titleNumber);
			$("#editDimen").val(data.dimensionName);
			$("#editvol").val(data.name);
			$("#editvolid").val(data.scaleTitleTypeid);
			$("#editDescribe").val(data.titleDescribe);
			form.render("select");
            layer.open({
				type: 1, 
			    title: '修改题目信息',
		        area: ['800px', '460px'],
		        shade: 0.8,
		        content: $('#editTitleFrom'),
		        cancel: function(){ 
				   $(".edittitle-con").css("display","none");
				}
		    });

	    	/*题目信息修改js*/
	    	$("#saveTitle").click(function(){
	    		var editTitleId=$("#editTitleId").val();
	    		var editTitleName=$("#editTitleName").val();
	    		var editSort=$("#editSort").val();
	    		var editvol=$("#editvolid").val();
	    		var editDescribe=$("#editDescribe").val();
	    		$.ajax({
	    			url: '../titlemanagement/updatetitle',  
	    	        type: 'get', 
	    	        dataType: 'json',  
	    	        data:{titleid:editTitleId,titlename:editTitleName,
	    	        	titlenumber:editSort,
	    	        	dimensionid:editvol,titledescribe:editDescribe},
	    	        success: function (data) {
	    	          if (data.code == 10001) {
	    	        	  layer.confirm("题目信息修改成功！", {
							  btn: ['确定'],icon:6
						  }, function(){
							  $(".edittitle-con").css("display","none");
				        	  layer.closeAll();
				        	  table.reload("titleid", { //此处是上文提到的 初始化标识id
		    		                where: {
		    		                	keyword:data.code=='10001'
		    		                }
		    		          });
						  });
	    	          } else {  
		    	          layer.alert('题目信息修改失败！', {icon: 2});
	    	        	  $("#editFrom").hide();
	    	          }  
	    	        }
	    		});  
	    	});	    	
		}
		if(obj.event === 'delete') {
			layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] 
	    		}, function(){
	    	    	$.ajax({
	        			url: '../titlemanagement/deletetitle',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{titleid: data.titleid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  layer.alert(getdata.msg, {icon: 1});
	        	        	  table.reload("titleid", { //此处是上文提到的 初始化标识id
					              where: {
					            	  keyword:getdata.code == '10001'
					              }
				            });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){
	        	        	layer.alert(getdata.msg, {icon: 2}); 
	        	        }
	        		});
	    		}, function(){ }
	    	);
		}
	});
	
	/*djf添加*/
	//监听反转操作（反转和正常）
	form.on('checkbox(isReverse)', function(obj) {
		var titleid=obj.value;
		if(obj.elem.checked){
			//反转
			$.ajax({
				type : 'get',
				url : '../titlemanagement/enableordisenablereverse?isreverse=1&titleid='+titleid,
				datatype : 'json',
				success : function(data) {
					if (data.code == 10001) {
						layer.confirm(data.msg, {
							  btn: ['确定']
						});
					} else {
						layer.confirm(data.msg, {
							  btn: ['确定']
						});
					}
				},
				error : function() {}
			});
		}else{
			//正常
			$.ajax({
				type : 'get',
				url : '../titlemanagement/enableordisenablereverse?isreverse=0&titleid='+titleid,
				datatype : 'json',
				success : function(data) {
					if (data.code == 10001) {
						layer.confirm(data.msg, {
							  btn: ['确定']
						});
					} else {
						layer.confirm(data.msg, {
							  btn: ['确定']
						});
					}
				},
				error : function() {}
			});
		}						
	});
	
	/*djf添加*/
	/*点击添加,添加一条选项信息*/
	$("#btnaddaoption").click(function(){
		$("#inpoptionscore").val("");
		$("#inpoptioncontent").val("");
		var index2=layer.open({
			type : 1,
			title : '单个选项添加',
			area : [ '560px', '380px' ],
			shade : 0.4,
			content : $('#adddivaoption'),
			btn : [ '保存'],
			yes : function() {
				var seloptionchange=$("#seloptionchange").val();  //选项排序号
				var seloptiontext=$("#seloptionchange").find("option:selected").text();  //选题名称A、B
				var inpoptionscore=$("#inpoptionscore").val().trim();  //选项分数
				var inpoptioncontent=$("#inpoptioncontent").val().trim();   //选项内容
				var optiontitleid=$("#inpoptiontitleid").val();
				var newcontent=seloptiontext+"、"+inpoptioncontent;
				if(seloptionchange==undefined || seloptionchange=="00"){
					layer.msg("请选择题目选项！",{icon:2});
					return false;
				}
				else if(inpoptionscore==null || inpoptionscore==""){
					layer.msg("请输入题目分数！",{icon:2});
					return false;
				}
				else if(inpoptioncontent==null || inpoptioncontent==""){
					layer.msg("请输入题目内容！",{icon:2});
					return false;
				}
				$.ajax({
        			url: '../titlemanagement/addoption',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{optionnumber: seloptionchange,optioncontent:newcontent,
        	        	optionscore:inpoptionscore,titleid:optiontitleid},
        	        success: function (getdata) {
        	          if (getdata.code == 10001) { 
        	        	  layer.confirm(getdata.msg, {
	    					  btn: ['确定'],
	    					  icon:1
	    				  }, function(){
	    					  layer.closeAll();
	    					  window.location.reload();
	    					  table.reload("taboptioncon", {
		    		                where: {
		    		                	keyword:getdata.code=='10001'
		    		                }
		    		          });	
	    				  });
        	          }else { 
        	        	  layer.alert(getdata.msg, {icon: 2});
        	          }  
        	        },
        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
        		});
			}
		});
	});
	/*表格工具栏事件*/
    table.on('tool(taboptioncon)', function(obj){
	    var data = obj.data;
	    //点击删除
	    if(obj.event==='delete'){
	    	layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] ,
	    		  icon:3
	    		}, function(){
	    	    	$.ajax({
	        			url: '../titlemanagement/deleteoption',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{optionid:data.optionid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  layer.confirm(getdata.msg, {
		    					  btn: ['确定'],
		    					  icon:1
		    				  }, function(){
		    					  layer.closeAll();
		    					  window.location.reload();
		    					  table.reload("tabexampaperid", {
			    		                where: {
			    		                	keyword:getdata.code=='10001'
			    		                }
			    		          });
		    				  });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
	        		});
	    		}, function(){}
	    	);
	    }
    });
	
});